﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Diagnostics;
using System.Collections.Specialized;

using Microsoft.Research.DynamicDataDisplay;
using Microsoft.Research.DynamicDataDisplay.DataSources;
using System.Collections.ObjectModel;
using Microsoft.Research.DynamicDataDisplay.Charts;
using WPFCharts;

namespace PerfCounterChart
{
	/// <summary>
	/// Interaction logic for Window1.xaml
	/// </summary>
	public partial class Window1 : Window
	{
        private ChartLine line = new ChartLine();
		public Window1()
		{
			InitializeComponent();

			Loaded += new RoutedEventHandler(Window1_Loaded);
            line.Color = Colors.Red;
            line.Label = "Memory";
            wpfChart.Primitives.Add(line);
		}

		private void Window1_Loaded(object sender, RoutedEventArgs e)
		{
			LineGraph chart = CreatePerformanceGraph("Memory", "Available MBytes");

			// uncomment two next lines to create a processor usage chart
			//CreatePerformanceGraph("Processor", "% Processor Time", "0");
			//CreatePerformanceGraph("Processor", "% Processor Time", "1");

			// these perf counters are device-specific, so
			// tune them to work on your machine and then uncommen
		}

		private LineGraph CreatePerformanceGraph(string categoryName, string counterName, string instanceName)
		{
			data = new PerformanceData(new PerformanceCounter(categoryName, counterName, instanceName));

			var filteredData = new FilteringDataSource<PerformanceInfo>(data, new MaxSizeFilter());

			var ds = new EnumerableDataSource<PerformanceInfo>(filteredData);
			ds.SetXMapping(pi => pi.Time.TimeOfDay.TotalSeconds);
			ds.SetYMapping(pi => pi.Value);
			//LineGraph chart = plotter.AddLineGraph(ds, 2.0, String.Format("{0} - {1}", categoryName, counterName));
			return null;
		}
		private LineGraph CreateFilteredPerformanceGraph(string categoryName, string counterName, string instanceName, IFilter<PerformanceInfo> filter)
		{
			data = new PerformanceData(new PerformanceCounter(categoryName, counterName, instanceName));

			var filteredData = new FilteringDataSource<PerformanceInfo>(data, filter);

			var ds = new EnumerableDataSource<PerformanceInfo>(filteredData);
			ds.SetXMapping(pi => pi.Time.TimeOfDay.TotalSeconds);
			ds.SetYMapping(pi => pi.Value);

			//LineGraph chart = plotter.AddLineGraph(ds, 2.0, String.Format("{0} - {1}", categoryName, counterName));
			return null;
		}

        private PerformanceData data;
		private LineGraph CreatePerformanceGraph(string categoryName, string counterName)
		{
			data = new PerformanceData(categoryName, counterName,line);
            data.CollectionChanged += new NotifyCollectionChangedEventHandler(data_CollectionChanged);
			var filteredData = new FilteringDataSource<PerformanceInfo>(data, new MaxSizeFilter());

			var ds = new EnumerableDataSource<PerformanceInfo>(filteredData);
			ds.SetXMapping(pi => pi.Time.TimeOfDay.TotalSeconds);
			ds.SetYMapping(pi => pi.Value);

			//LineGraph chart = plotter.AddLineGraph(ds, 2.0, String.Format("{0} - {1}", categoryName, counterName));
			return null;
		}

        void data_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            wpfChart.RedrawPlotLines();
        }
	}
}
